﻿using System;
using System.ComponentModel;
using System.Web.UI;
using System.Drawing.Design;
using System.Web.UI.WebControls;

namespace Technullogy.Web.UI
{
    [Designer(typeof(RoundedDivControlDesigner))]
    [ParseChildren(false)]
    [ToolboxData("<{0}:RoundedDiv runat=\"server\"></{0}:RoundedDiv>")]
    public class RoundedDiv : CompositeControl, INamingContainer
    {
        #region Properties
        [Description("The text displayed at the top of RoundedDiv."), Category("Appearance")]
        public String Title { get; set; }

        private bool _enableCollapsibleContent = true;
        [Description("Enables the ability to expand and collapse the content div"), Category("Behavior"), DefaultValue(true)]
        public bool EnableCollapsibleContent
        {
            get { return _enableCollapsibleContent; }
            set { _enableCollapsibleContent = value; }
        }

        private RoundedDivLayout _defaultLayout = RoundedDivLayout.Expanded;
        [Description("When EnableCollapsibleContent is True, this property sets the default layout behavior."), Category("Behavior"), DefaultValue(RoundedDivLayout.Expanded)]
        public RoundedDivLayout DefaultLayout
        {
            get { return _defaultLayout; }
            set { _defaultLayout = value; }
        }

        private RoundedDivSkin _embeddedSkin = RoundedDivSkin.Green;
        [Description("The embedded skin used by RoundedDiv. Set EnableEmbeddedSkins to true to use embedded skins.  Default is Green."), Category("Appearance"), DefaultValue(RoundedDivSkin.Green)]
        public RoundedDivSkin EmbeddedSkin
        {
            get { return _embeddedSkin; }
            set { _embeddedSkin = value; }
        }
        [Description("The custom skin used by RoundedDiv. Set EnableEmbeddedSkins to false to use custom skins."), Category("Appearance")]
        public String CustomSkin { get; set; }

        [Description("The image displayed at the top of RoundedDiv."), Category("Appearance"), Editor("System.Web.UI.Design.ImageUrlEditor,System.Design,Version=2.0.0.0,Culture=neutral,PublicKeyToken=b03f5f7f11d50a3a", typeof(UITypeEditor))]
        public String TitleImageUrl { get; set; }

        private bool _roundTopLeft = true;
        [Description("Whether or not to make the top left corner rounded."), Category("Appearance"), DefaultValue(true)]
        public bool RoundTopLeft
        {
            get { return _roundTopLeft; }
            set { _roundTopLeft = value; }
        }

        private bool _roundTopRight = true;
        [Description("Whether or not to make the top right corner rounded."), Category("Appearance"), DefaultValue(true)]
        public bool RoundTopRight
        {
            get { return _roundTopRight; }
            set { _roundTopRight = value; }
        }

        private bool _roundBottomLeft = true;
        [Description("Whether or not to make the bottom left corner rounded."), Category("Appearance"), DefaultValue(true)]
        public bool RoundBottomLeft
        {
            get { return _roundBottomLeft; }
            set { _roundBottomLeft = value; }
        }

        private bool _roundBottomRight = true;
        [Description("Whether or not to make the bottom right corner rounded."), Category("Appearance"), DefaultValue(true)]
        public bool RoundBottomRight
        {
            get { return _roundBottomRight; }
            set { _roundBottomRight = value; }
        }

        private bool _enableEmbeddedSkins = true;
        [Description("Whether to use embedded skins or not.  Set to false to use a custom skin."), Category("Appearance"), DefaultValue(true)]
        public bool EnableEmbeddedSkins
        {
            get
            {
                return _enableEmbeddedSkins;
            }
            set
            {
                _enableEmbeddedSkins = value;
            }
        }

        private String Skin
        {
            get
            {
                if (EnableEmbeddedSkins)
                    return _embeddedSkin.ToString();
                else
                    return CustomSkin;
            }
        }
        #endregion

        #region Methods
        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
            if (EnableEmbeddedSkins)
            {
                // Register CSS
                String styleSheet = String.Format("Technullogy.Web.UI.RoundedDiv.Skins.{0}.styles.css", Skin);
                String includeLocation = Page.ClientScript.GetWebResourceUrl(typeof(RoundedDiv), styleSheet);
                String cssLink = String.Format("<link rel=\"stylesheet\" text=\"text/css\" href=\"{0}\" />", includeLocation);
                if (!Page.ClientScript.IsClientScriptBlockRegistered(GetType(), styleSheet))
                    Page.ClientScript.RegisterClientScriptBlock(GetType(), styleSheet, cssLink);
            }

            // Register Scripts
            if (_enableCollapsibleContent)
            {
                String scriptName = "Technullogy.Web.UI.RoundedDiv.Scripts.RoundedDiv.js";
                Page.ClientScript.RegisterClientScriptResource(typeof(RoundedDiv), scriptName);
            }
        }
        protected override void RenderContents(HtmlTextWriter writer)
        {
            String imgExpand = String.Format("Technullogy.Web.UI.RoundedDiv.Skins.{0}.Img.expand.gif", _embeddedSkin);
            String locExpand = Page.ClientScript.GetWebResourceUrl(typeof(RoundedDiv), imgExpand);

            String imgCollapse = String.Format("Technullogy.Web.UI.RoundedDiv.Skins.{0}.Img.collapse.gif", _embeddedSkin);
            String locCollapse = Page.ClientScript.GetWebResourceUrl(typeof(RoundedDiv), imgCollapse);

            String contentDivId = String.Format("{0}_RcContentDiv", ClientID);
            String contentClass = String.Format("{0}RcContent", Skin);
            String collapsibleContent = String.Empty;

            // Check if collapse features are enabled
            if (_enableCollapsibleContent)
            {
                String tooltip = "title=\"Press Ctrl+click to toggle all\"";

                collapsibleContent = String.Format("<img src=\"{2}\" onclick=\"rcToggle(event, '{0}', '{1}', '{2}', '{3}');\" class=\"{1}RcToggleHide\" {4}/>", contentDivId, Skin, locCollapse, locExpand, tooltip);

                // Do we show or hide content by default?
                if (_defaultLayout == RoundedDivLayout.Collapsed)
                {
                    contentClass = String.Format("{0}RcHide", Skin);
                    collapsibleContent = String.Format("<img src=\"{3}\" onclick=\"rcToggle(event, '{0}', '{1}', '{2}', '{3}');\" class=\"{1}RcToggleShow\" {4}/>", contentDivId, Skin, locCollapse, locExpand, tooltip);
                }
            }

            // Setup ImageUrl String
            String formattedImageUrl = (!String.IsNullOrEmpty(TitleImageUrl) ? String.Format("<img src=\"{0}\" title=\"{1}\" alt=\"{1}\" class=\"{2}RcImage\" />", TitleImageUrl, Title, Skin) : String.Empty);

            String tableWidth = (!Width.IsEmpty ? String.Format("<table width=\"{0}\"><tr><td>", Width) : String.Empty);
            String topLeftCss = (_roundTopLeft ? String.Format("<div class=\"{0}RcTopLeft\">", Skin) : String.Empty);
            String topRightCss = (_roundTopRight ? String.Format("<div class=\"{0}RcTopRight\">", Skin) : String.Empty);
            String botLeftCss = (_roundBottomLeft ? String.Format("<div class=\"{0}RcBotLeft\">", Skin) : String.Empty);
            String botRightCss = (_roundBottomRight ? String.Format("<div class=\"{0}RcBotRight\">", Skin) : String.Empty);

            // Opening tags
            // Had to use a table to get the width to work right.
            writer.Write(tableWidth);
            writer.Write(String.Format("<div class=\"{0}RcBase\">", Skin));
            writer.Write(String.Format("<div class=\"{0}RcTop\">", Skin));
            writer.Write(String.Format("<div class=\"{0}RcRight\">", Skin));
            writer.Write(String.Format("<div class=\"{0}RcBot\">", Skin));
            writer.Write(String.Format("<div class=\"{0}RcLeft\">", Skin));

            writer.Write(topLeftCss);
            writer.Write(topRightCss);
            writer.Write(botLeftCss);
            writer.Write(botRightCss);

            writer.Write(String.Format("{0}<div class=\"{1}RcTitle\">{2}<h3>{3}</h3>", collapsibleContent, Skin, formattedImageUrl, Title));
            writer.Write(String.Format("<div class=\"{0}\" id=\"{1}\" collapsible=\"{2}\">", contentClass, contentDivId, EnableCollapsibleContent));
            base.RenderContents(writer);
        }

        public override void RenderEndTag(HtmlTextWriter writer)
        {
            // Check how many divs we need to close - minimum is 7
            int divsToClose = 7;
            if (_roundTopLeft)
                divsToClose += 1;

            if (_roundTopRight)
                divsToClose += 1;

            if (_roundBottomLeft)
                divsToClose += 1;

            if (_roundBottomRight)
                divsToClose += 1;

            for (int x = 0; x < divsToClose; x += 1)
            {
                writer.Write("</div>");
            }

            // If width is set, we need to close off the table
            if (!Width.IsEmpty)
                writer.Write("</td></tr></table>");

            base.RenderEndTag(writer);
        }
        #endregion
    }
}
